package baiqitun.stupro.api.controller;

import baiqitun.stupro.api.model.DepartModel;
import baiqitun.stupro.api.model.UserInfo;
import baiqitun.stupro.common.model.Result;
import baiqitun.stupro.common.model.TreeModel;
import baiqitun.stupro.common.model.vo.TreeNodeVo;
import baiqitun.stupro.common.util.ResultUtil;
import baiqitun.stupro.security.entity.SysDepart;
import baiqitun.stupro.security.entity.SysUser;
import baiqitun.stupro.security.service.SysDepartService;
import baiqitun.stupro.security.service.SysDepartUserService;
import baiqitun.stupro.subject.service.SysStudentInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author kedaji
 */
@Api(description = "统计（汇总）控制器")
@RequestMapping("/census")
@RestController
public class CensusController {
    @Autowired
    private SysStudentInfoService sysStudentInfoService;
    @Autowired
    private SysDepartUserService sysDepartUserService;
    @Autowired
    private SysDepartService sysDepartService;

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("统计所有选择某导师的学生（每一届选择某导师的学生）")
    @GetMapping("/selectedTutorForStudentOfEveryYear")
    public Result<?> censusSelectedThisTutorForStudentOfEveryYear(@ApiParam("导师用户id") @PathVariable("id") String id){
        List<Integer> years = sysStudentInfoService.years();
        Map<Integer, List<UserInfo>> studentListOfEveryYear = new LinkedHashMap<>();
        for (Integer year: years){
            List<UserInfo> studentOfYear = censusSelectedTutorStudentOfYear(id, year);
            studentListOfEveryYear.put(year, studentOfYear);
        }
        return ResultUtil.ok(studentListOfEveryYear);
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("统计某届选择某导师的学生")
    @GetMapping("/selectedTutorForStudentOfYear")
    public Result<?> censusSelectedTutorForStudentOfYear(@ApiParam("导师用户id") @PathVariable("id") String id, Integer year){
        return ResultUtil.ok(censusSelectedTutorStudentOfYear(id, year));
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("统计某系在某届所有未选择导师的学生(根据学生所在班级进行汇总)")
    @GetMapping("/unselectedTutorStudentForDepartOfYear")
    public Result<?> censusUnselectedTutorStudentForDepartOfYear(String departId, Integer year){
        // 获取到了当前系以及系下的班级
        TreeModel<SysDepart> departTree = sysDepartService.getTree(departId);
        List<TreeModel<SysDepart>> classTrees = departTree.getChildrenTree();

        List<DepartModel> classModels = new LinkedList<>();
        for (TreeModel<SysDepart> classTree: classTrees){
            DepartModel classModel = new DepartModel();
            List<SysUser> students = sysStudentInfoService.listStudentForClassOfYear(classTree.getNode().getId(), year);
            List<UserInfo> studentsInfo = new LinkedList<>();
            for (SysUser tmpUser: students){
                UserInfo studentInfo = new UserInfo();
                studentInfo.setName(tmpUser.getRealName()).setUsername(tmpUser.getUsername());
            }
            classModel.setUsers(studentsInfo).setDepartId(classTree.getNode().getId()).setDepartName(classTree.getNode().getName());
            classModels.add(classModel);
        }
        return ResultUtil.ok(classModels);
    }

    private List<UserInfo> censusSelectedTutorStudentOfYear(String tutorUserId, Integer year){
        List<SysUser> students = sysStudentInfoService.listStudent(tutorUserId, year);
        List<UserInfo> studentUserInfoList = new LinkedList<>();
        for (SysUser student: students){
            UserInfo studentUserInfo = new UserInfo();
            List<SysDepart> departs = sysDepartUserService.listDepartForUser(student.getId());
            studentUserInfo.setName(student.getRealName()).setUserId(student.getId()).setDeparts(departs);
            studentUserInfoList.add(studentUserInfo);
        }
        return studentUserInfoList;
    }
}
